import { useTitle } from '@vueuse/core'
import { Router } from 'vue-router'
import { ElMessage } from 'element-plus'
/**
 * 路由守卫函数
 * @param router - 路由实例
 */
export function createRouterGuard(router: Router) {
  // router.beforeEach(async (to: any) => {
  //   window.document.title = window.ApiDev.title
  //   if (to.meta.hasAuth) {
  //     if (localStorage.getItem('token')) {
  //     }else{
  //       ElMessage.error('请重新登录')
  //       router.replace('/')
  //     }
  //   }else{
  //     window.$loadingBar?.start()
  //   }
  // })
  // router.afterEach((to: any) => {
  //   useTitle(to.meta.title)
  //   window.$loadingBar?.finish()
  // })

  router.beforeEach((to, from, next) => {
    const token = window.sessionStorage.getItem('token')
    if (token) {
      next()
    } else if (to.path !== '/') {
      //跳转到登入页
      next({ path: '/' })
    } else {
      next()
    }
    // next()
  })
}
